/*带背景的输入布局
* 背景：默认白色
* 背景形状：使用inputview.dart
* 输入类型：1.手机号，2.密码，3.验证码
* 展示样式类型：
* （1）手机号
* LEFT_PIC.左侧带图标，LEFT_TEXT.左侧显示"+86"
* */

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:frametrim/utility/sq_color.dart';

class TextFieldView extends StatefulWidget {
  //布局样式  LEFT_PIC  LEFT_TEXT  NONE
  late String inputStyle;

  //输入内容  PHONE(手机号)  PSW(密码)  YZM(验证码)   TEXT(文本内容)
  late String inputType;

  //背景边框样式  CIRCLE(圆形)  RoundedRectangle(圆角矩形)
  late String? borderType;

  //左侧图标  传入Image,如果不显示就传入一个Container()
  late Widget? imageLeft;
  late Widget? imageRight; //右侧小眼睛
  TextEditingController controller = TextEditingController();

  //hite文本
  late String? hiteText;
  late double? fontSize;
  late Color hiteColor;
  late Color? textColor;

  //密码右侧小眼睛
  late Widget? openEyes;
  late Widget? closeEyes;

  TextFieldView(
      {this.inputStyle = "LEFT_PIC",
      this.inputType = "PHONE",
      this.borderType = "CIRCLE",
      this.fontSize = 15,
      this.imageLeft,
      this.imageRight,
      this.openEyes,
      this.closeEyes,
      this.hiteColor = const Color(0xffaeaeae),
      this.textColor = const Color(0xff000000),
      required this.hiteText,
      required this.controller});

  @override
  State<StatefulWidget> createState() => TextFieldPhoneState();
}

class TextFieldPhoneState extends State<TextFieldView> {
  //输入类型
  late TextInputType textInputType;
  late int maxLength;

  //输入限制
  late List<TextInputFormatter>? inputFormatters;
  bool isOpenEye = false;

  @override
  void initState() {
    super.initState();

  }

  @override
  Widget build(BuildContext context) {

    return Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: buildTextFiled());
  }

  List<Widget> buildTextFiled() {
    buildType();
    List<Widget> children = [];
    if (widget.inputType != "YZM") {
      //左侧
      if (widget.inputStyle == 'LEFT_PIC') {
        children.add(widget.imageLeft ?? Container());
      } else if (widget.inputStyle == 'LEFT_TEXT') {
        children.add(Row(
          children: <Widget>[
            SizedBox(width: 10),
            Text(
              '+86',
              style: TextStyle(
                  fontSize: widget.fontSize,
                  color: SQColor.color_222222,
                  fontWeight: FontWeight.bold),
            ),
            SizedBox(width: 10)
          ],
        ));
      }
    }
    //输入框
    children.add(Expanded(
      child: TextField(
        maxLength: maxLength,
        //键盘样式
        keyboardType: textInputType,
        //输入限制
        inputFormatters: inputFormatters,
        controller: widget.controller,
        /*是否加密*/
        obscureText: !isOpenEye,

        decoration: InputDecoration(
          // 去除输入框底部的字符计数,例如11/20
          counterText: '',
          /*去掉下划线*/
          // border: InputBorder.none,
          //contentPadding+border 设置TextField垂直居中
          contentPadding: EdgeInsets.symmetric(vertical: 0),
          border: OutlineInputBorder(borderSide: BorderSide.none),
          hintText: widget.hiteText,
          hintStyle:
              TextStyle(fontSize: widget.fontSize, color: widget.hiteColor),
          labelStyle:
              TextStyle(fontSize: widget.fontSize, color: widget.textColor),
        ),
      ),
    ));
    if (widget.inputType == "PSW") {
      children.add(buildEye());
    }
    return children;
  }

  /*右侧小眼睛*/
  Widget buildEye() {
    return GestureDetector(
      onTap: () {
        setState(() {
          isOpenEye = !isOpenEye;
          print('isOpenEye==' + isOpenEye.toString());
        });
      },
      child: isOpenEye ? widget.openEyes : widget.closeEyes,
    );
  }

  void buildType(){
    if (widget.inputType == "PHONE") {
      maxLength = 11;
      textInputType = TextInputType.number;
      inputFormatters = [FilteringTextInputFormatter.allow(RegExp("[0-9]"))];
      isOpenEye = true;
    } else if (widget.inputType == "PSW" ) {
      maxLength = 20;
      textInputType = TextInputType.text;
      inputFormatters = [
        FilteringTextInputFormatter.allow(RegExp("[a-zA-Z]|[\u4e00-\u9fa5]|[0-9]"))
      ];
      isOpenEye = false;
    } else if (widget.inputType == "YZM") {
      maxLength = 6;
      textInputType = TextInputType.number;
      inputFormatters = [FilteringTextInputFormatter.allow(RegExp("[0-9]"))];
      isOpenEye = true;
    } else  if ( widget.inputType == "TEXT") {
      maxLength = 20;
      textInputType = TextInputType.text;
      inputFormatters = [
        FilteringTextInputFormatter.allow(RegExp("[a-zA-Z]|[\u4e00-\u9fa5]|[0-9]"))
      ];
      isOpenEye = true;
    }
    print("textInputType=="+textInputType.toString());
  }
}
